/**
 * 
 */
package MCQReaderModel;

import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeModel;

import MCQReaderInterface.MainInterface;

/**
 * @author barrolemax
 * 
 */
public class ModelManager {

	public static void save(String filePath) {
		try {
			FileOutputStream fos = new FileOutputStream(filePath);
			XMLEncoder o = new XMLEncoder(new BufferedOutputStream(fos));
			o.writeObject(MainInterface.getThisIsIt().getLeftFrame().getTree()
					.getMainTree().getModel());
			o.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void load(String filePath) {
		try {
			XMLDecoder d = new XMLDecoder(new BufferedInputStream(
					new FileInputStream(filePath)));
			MainInterface.getThisIsIt().getLeftFrame().getTree().getMainTree()
					.setModel((TreeModel) d.readObject());
			MainInterface.getThisIsIt().getLeftFrame().getTree().getMainTree()
					.treeDidChange();
			d.close();
		} catch (FileNotFoundException ex) {
			ex.printStackTrace();
		}
		MainInterface.getThisIsIt().getLeftFrame().getTree().setRacine(
				(DefaultMutableTreeNode) MainInterface.getThisIsIt()
						.getLeftFrame().getTree().getMainTree().getModel()
						.getRoot());
	}
}
